package predicates

import (
	"sigs.k8s.io/controller-runtime/pkg/event"
	"sigs.k8s.io/controller-runtime/pkg/predicate"
)

func AND(predicates ...predicate.Funcs) predicate.Funcs {
	return predicate.Funcs{
		UpdateFunc: func(e event.UpdateEvent) bool {
			for _, p := range predicates {
				if p.UpdateFunc != nil && !p.UpdateFunc(e) {
					return false
				}
			}
			return true
		},
		CreateFunc: func(e event.CreateEvent) bool {
			for _, p := range predicates {
				if p.CreateFunc != nil && !p.CreateFunc(e) {
					return false
				}
			}
			return true
		},
		DeleteFunc: func(e event.DeleteEvent) bool {
			for _, p := range predicates {
				if p.DeleteFunc != nil && !p.DeleteFunc(e) {
					return false
				}
			}
			return true
		},
		GenericFunc: func(e event.GenericEvent) bool {
			for _, p := range predicates {
				if p.GenericFunc != nil && !p.GenericFunc(e) {
					return false
				}
			}
			return true
		},
	}
}

func OR(predicates ...predicate.Funcs) predicate.Funcs {
	return predicate.Funcs{
		UpdateFunc: func(e event.UpdateEvent) bool {
			for _, p := range predicates {
				if p.UpdateFunc != nil && p.UpdateFunc(e) {
					return true
				}
			}
			return false
		},
		CreateFunc: func(e event.CreateEvent) bool {
			for _, p := range predicates {
				if p.CreateFunc != nil && p.CreateFunc(e) {
					return true
				}
			}
			return false
		},
		DeleteFunc: func(e event.DeleteEvent) bool {
			for _, p := range predicates {
				if p.DeleteFunc != nil && p.DeleteFunc(e) {
					return true
				}
			}
			return false
		},
		GenericFunc: func(e event.GenericEvent) bool {
			for _, p := range predicates {
				if p.GenericFunc != nil && p.GenericFunc(e) {
					return true
				}
			}
			return false
		},
	}
}
